package com.maodou.android.viewmodel.setting

import androidx.lifecycle.viewModelScope
import com.maodou.android.data.model.ContactInfoVo
import com.maodou.android.data.remote.UserRepository
import com.maodou.android.utils.Constants.APP_CONFIG_CONTACT
import com.maodou.core.base.BaseViewModel
import com.maodou.core.manager.GlobalStateManager
import com.maodou.core.navigation.GlobalUiBus
import com.maodou.core.navigation.Navigator3
import com.maodou.core.network.handleResult
import com.maodou.core.network.toUserMessage
import com.squareup.moshi.Moshi
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * @Author Xing
 * @CreateTime 2025年09月25日
 * @Description 抽屉、设置相关页面业务
 */
@Suppress("UNCHECKED_CAST")
@HiltViewModel
class SettingViewModel @Inject constructor(
    private val globalState: GlobalStateManager,
    private val repository: UserRepository,
) : BaseViewModel<SettingUiState, SettingUiEvent>() {

    init {
        getContactConfig()
    }

    override fun createInitialState(): SettingUiState {
       return SettingUiState()
    }


    fun getContactConfig(){
        updateUiState {
            val config = globalState.appConfig.value[APP_CONFIG_CONTACT]
            var contactInfoList:List<ContactInfoVo> = arrayListOf()
            if(config is List<*>) {
                contactInfoList = config.map { map ->
                    if(map is Map<*,*>) ContactInfoVo(
                        imgUrl = map["imgUrl"] as? String,
                        link = map["link"] as? String,
                        subtitle = map["subtitle"] as? String,
                        title = map["title"] as? String
                    ) else ContactInfoVo()
                }
            }
            it.copy(contact = contactInfoList,)
        }
    }

    fun submitFeedback(contact:String,feedback:String){
        viewModelScope.launch {
            repository.onCreateFeedback(contact,feedback).handleResult(
                onSuccess = {
                    GlobalUiBus.showAlertDialog(
                        title = "提交成功！${if (contact.isNotEmpty()) "\n若有必要客服会通过联系方式联系你！" else ""}",
                        positiveText = "知道了",
                        cancelable = false,
                        onConfirm = {
                            Navigator3.pop()
                        }
                    )
                },
                onError = { e ->
                    GlobalUiBus.showToast(e.toUserMessage())
                },
                onLoading = { load ->
                    updateUiState {
                        it.copy(
                            isLoading = load,
                        )
                    }
                }
            )
        }
    }


}


/**
 * UI 状态
 */
data class SettingUiState(
    val isLoading: Boolean = false,
    val contact: List<ContactInfoVo>? = null
)

/**
 * UI 事件
 */
sealed interface SettingUiEvent {
    data class ShowError(val message: String) : SettingUiEvent
}
